// pages/gangcircle/gangcircle.js
const api = require('../../util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        miniProgramtype: app.globalData.miniProgramtype,
        active: 0,
        list: [],//测试列表
        top: 0,
        potop: 0,
        wzlist: [],
        videolists: [],
        finderusername: '',
        is_bind_phone: '',
    },
    getVideoId() {
        var that = this
        api.http('/common/getVideoId', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                finderusername: res.data.finder_user_name
            })
          }
        })
    },
    // 点击观看
    channelfun() {
        var that = this
        api.http('/UserIntegral/AddIntegral', 'POST', false, {
            type: 4
        }, function (res) {
        })
    },
    // 热门视频
    popularSelectedVideos(e) {
        var that = this
        api.http('/index/popularSelectedVideos', 'POST', false, {
        }, function (res) {
          if(res.code == 200) {
            that.setData({
                videolists: res.data
            })
          }
        })
    },
    // 获取文章分类
    getArticleCategory(e) {
        var type = false
        if (typeof(e)=='undefined') {
            type = true
        }
        var that = this
        api.http('/article/getArticleCategory', 'POST', type, {
        }, function (res) {
            if(res.code == 200) {
                that.setData({
                    list: res.data
                }, () => {
                    if (that.data.list.length>0) {
                        that.getArticleList(that.data.list[0].id,e)
                    }
                    let query = wx.createSelectorQuery()
                    query.select('.tabs').boundingClientRect((rect) => {
                        that.setData({
                            potop: rect.top
                        })
                    }).exec()
                })
            }
        })
    },
    // 文章详情
    getArticleList(e,type) {
        var that = this
        var category_id = ''
        if (typeof(e)!='undefined'&&this.data.active===0) {
            category_id = e
        } else {
            category_id = this.data.list[this.data.active].id
        }
        var type = false
        if (typeof(e)=='undefined') {
            type = true
        }
        api.http('/article/getArticleList', 'POST', type, {
            category_id
        }, function (res) {
            if(res.code == 200) {
                that.setData({
                    wzlist: res.data
                })
            }
        })
    },
    onPageScroll:function(e){
        this.setData({
            top: e.scrollTop
        })
    },
    // 跳转精选视频
    tofeaturedvideos() {
        wx.navigateTo({
            url: '/pages/featuredvideos/featuredvideos'
        })
    },
    // tab切换
    funactive(e) {
        this.setData({
            active: e.currentTarget.dataset.index
        }, () => {
            this.getArticleList()
        })
    },
    // 跳转推荐详情
    toarticledetails(e) {
        wx.navigateTo({
            url: '/pages/articledetails/articledetails?id='+e.currentTarget.dataset.id
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        if (wx.getStorageSync(app.globalData.miniProgramtype+'access_token')=='') {
            app.wxlogin().then(()=>{
                that.setData({
                    is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
                })
            })
        } else {
            that.setData({
                is_bind_phone: wx.getStorageSync(app.globalData.miniProgramtype+'is_bind_phone')
            })
        }
    },
    getPhoneNumber (e) {
        var that = this
        if (e.detail.errMsg != 'getPhoneNumber:fail no permission'&&e.detail.errMsg != 'getPhoneNumber:fail user deny') {
            console.log(e)
            api.http('/login/bindPhone', 'POST', false, {
                code: e.detail.code
            }, function (res) {
              if(res.code == 200) {
                 that.setData({
                    is_bind_phone: 1
                })
                wx.setStorageSync(app.globalData.miniProgramtype+'is_bind_phone',1)
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1000
                })
              } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
              }
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                select: 2
            })
        }
        this.popularSelectedVideos()
        this.getArticleCategory(1)
        this.getVideoId()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    // 分享到朋友圈
    onShareTimeline(){
        return {
          title: '帮帮圈',
        // query: 'id=' + this.id,
        // imageUrl: ''
        }
    }
})